<template>
  <a-select v-model="curValue" :placeholder="enumData.Name" @change="handleChange" @select="handleSelected" v-bind="$attrs">
    <a-select-option v-for="item in enumData.EnumItems" :key="item.Code" :value="item.Code">{{ item.Name }}</a-select-option>
  </a-select>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  props: {
    code: String,
    value: String
  },
  data() {
    return {
      curValue: undefined,
      enumData: {}
    }
  },
  computed: {
  },
  watch: {
    value(value) {
      this.curValue = value
    }
  },
  mounted() {
    this.curValue = this.value
    this.getEnum({ code: this.code }).then(result => {
      this.enumData = result
    })
  },
  methods: {
    ...mapActions({ getEnum: 'getEnum' }),
    handleChange(val) {
      if (!val) {
        this.$emit('input', undefined)
      }
    },
    handleSelected(val) {
      this.$emit('input', val)
      var item = this.enumData.EnumItems.find(w => w.Code === val)
      this.$emit('select', val, item)
    }
  }
}
</script>

<style>
</style>
